<template>
	<view class="collection-container">
		<view class="list">
			<view class="list-scroll-container">
				<!-- scrolltolower事件可以监听加载到底部 -->
				<scroll-view scroll-y="true" class="list-scroll" @scrolltolower="loadMore">
					<view v-for="(item,index) in list" :key="index">
						<view class="list-card">
							<view @click="goDetail(item._id)">
								<view class="list-card-top">
									<text class="txt">{{item.title}}</text>
									<view @click.stop="collect(item._id)">
										<uni-icons :type="isLike(item._id)" size="30" color="#ff6600"></uni-icons>
									</view>
								</view>
								<view class="list-card-middle">
									<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
										<swiper-item v-for="(demo,id) in item.imgArr" :key="id">
											<image :src="demo" mode=""></image>
										</swiper-item>
									</swiper>
								</view>
								<view class="list-card-bottom">
									<view class="article-type">
										{{item.province}}
									</view>
									<view class="browser-number">
										{{item.scan}} 浏览
									</view>
								</view>
							</view>
						</view>
					</view>
					<uni-load-more v-if="list.length > 5 && isShowLoadMore" :status="loadData || 'loading'">
					</uni-load-more>
				</scroll-view>
			</view>
		</view>
		<view class="no-data" v-if="list.length === 0">
			暂无查询结果
		</view>
	</view>
</template>

<script>
	import {getDetail} from "../../ajax/api/qiye.js";
	import {
		collectInfo
	} from "../../ajax/api/home.js";
	import {mapState,mapMutations} from "vuex";
	export default {
		data() {
			return {
				list:[],
				collectIds : [],
				isShowLoadMore:true,
				loadData:"loading"
			}
		},
		computed:{
			...mapState(["userInfo"])
		},
		async onShow(){
			this.list = []
			if (this.token) {
				// 有token调用whoami
				const userInfo = await whoami();
				this.updateUserInfo(userInfo);
			}
			this.collectIds = this.userInfo.collect;
			this.collectIds.forEach(async (item,index)=>{
				const res = await getDetail(item);
				this.list.push(res);
			})
		},
		methods: {
			...mapMutations(["updateUserInfo"]),
			// 进入详情页
			goDetail(id){
				this.$Router.push({
					name: "detail",
					params: {
						id,
					}
				})
			},
			// 收藏
			async collect(id) {
				if (this.userInfo) {
					// 登录状态
					if (this.userInfo.collect) {
						// 有收藏状态
						let collect = JSON.parse(JSON.stringify(this.userInfo.collect));
						let isCollect = false;
						// 判断是否已经收藏
						collect.forEach(async (item, index) => {
							if (item === id) {
								// 表示有已经收藏的，取消收藏
								collect.splice(index, 1);
								isCollect = true;
								const res = await collectInfo({
									collect
								})
							}
						})
						// 没有收藏过
						if (!isCollect) {
							collect.push(id)
							const res = await collectInfo({
								collect
							})
						}
						// 更新userInfo
						const userInfo = {
							...this.userInfo,
							collect
						};
						this.updateUserInfo(userInfo);
					} else {
						// 第一次进行收藏
						let collect = [id];
						const userInfo = {
							...this.userInfo,
							collect
						};
						const res = await collectInfo({
							collect,
						})
			
						// 更新userInfo
						this.updateUserInfo(userInfo);
					}
				} else {
					// 没有登录就跳转到登录界面
					uni.navigateTo({
						url: "/pages/login/login"
					})
				}
			},
			isLike(id){
				if(this.userInfo && this.userInfo.collect.includes(id)){
					return "heart-filled"
				}else{
					return "heart"
				}
			},
			// 加载更多
			async loadMore() {
				this.isShowLoadMore = true;
				this.current++;
				const res = await getInfo({
					current: this.current,
					pageSize: this.pageSize,
					status: this.status
				})
				this.list.push(res.rows);
				this.isShowLoadMore = false;
				if (res.count === list.length) {
					// 全部数据加载完成
					this.loadData = "no-more";
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "./css/collection.scss";
</style>
